package patrol.mobile.com.mobilepatrol.activity.monitor;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.Gson;

import java.util.HashMap;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.iankkElefailinsprecListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by z on 2018/9/18.
 * Des: 机电设备巡检维护
 */

public class JdsbxjwhActivity extends CommonListActivity implements View.OnClickListener, OnRecyclerViewItemClickListener {
    private iankkElefailinsprecListBean listBean;
    private boolean isShowAddIcon = false;
    private String orgId;

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new MyAdapter(this);
    }

    @Override
    protected String initTitleName() {
        if(isShowAddIcon){
            return "机电设备巡检维护";
        }else {
            return "机电设备巡检";
        }
    }

    @Override
    public void initData() {
        super.initData();
        isShowAddIcon = getIntent().getBooleanExtra("isShowAddIcon", false);
        orgId = getIntent().getStringExtra("orgId");
        mAdapter.setOnItemClickListener(this);
        iv_right_title.setImageResource(R.mipmap.add_white);
        iv_right_title.setVisibility(isShowAddIcon ? View.VISIBLE : View.GONE);
        iv_right_title.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getHttpData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_right_title:
                startActivity(JdxjEditActivity.class);
                break;
        }
    }


    //获取设备巡检列表接口
    private void getHttpData() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("userId", UserManager.getUserId());
        hashMap.put("orgId", orgId);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getJiankkElefailinsprecList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                listBean = new Gson().fromJson(response, iankkElefailinsprecListBean.class);
                mAdapter.updateItems(listBean.getList());
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        if (null != listBean) {
            startActivity(new Intent(this, JdxjDetailActivity.class)
                    .putExtra("id", listBean.getList().get(position).getId()));
        }

    }

    public class MyAdapter extends BaseQuickAdapter<iankkElefailinsprecListBean.ListBean> {

        public MyAdapter(Context context) {
            super(context);
        }

        @Override
        protected int attachLayoutRes() {
            return R.layout.item_jdxjwh_layout;
        }

        @Override
        protected void convert(BaseViewHolder holder, iankkElefailinsprecListBean.ListBean item) {
            holder.setText(R.id.itemPostion, listBean.getList().indexOf(item) + 1 + "");
            holder.setText(R.id.itemJdsbxjSfzName, item.getOrg().getOrgName());
            holder.setText(R.id.itemJdsbxjRen, "巡检人:" + item.getAccount().getName());
            holder.setText(R.id.itemJdsbxjTime, item.getDate());
            holder.setText(R.id.itemJdsbxjType, item.getAuditName());

        }
    }
}
